package Client_logic;

/**
 * This class controls Product details (Add/Update/Delete)
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class Product{
	
	/** Product ID */
	private int ProdID;
	
	/** Product Price */
	private double ProdPrice;
	
	/** Product Name */
	private String ProdName;
	
	/** Product Description */
	private String ProdDescription;
	
	/** Category ID */
    private long CategoryID;
	
	public Product(){
		this.ProdID = 0;
		this.ProdPrice = 0;
		this.ProdName = null;
		this.ProdDescription = null;
		this.CategoryID = 0;
	}
    
    /**
     * @param ProdID Product ID
	 * @param ProdPrice Product Price
	 * @param ProdName Product Name
	 * @param ProdDescription Product Description
	 * @param CategoryID Product CategoryID
	 */
	public Product(int ProdID, double ProdPrice, String ProdName, String ProdDescription, long CategoryID) {
		super();
		this.ProdID = ProdID;
		this.ProdPrice = ProdPrice;
		this.ProdName = ProdName;
		this.ProdDescription = ProdDescription;
		this.CategoryID = CategoryID;
	}

	/**
	 * @return the ProdID
	 */
	public int GetprodID(){
		return this.ProdID;
	}
	
	/**
	 * @param ProdID the product ID to set
	 */
	public void SetprodID(int ProdID){
		this.ProdID = ProdID;
	}
	
	/**
	 * @return the ProdPrice
	 */
	public double getPrice(){
		return ProdPrice;
	}

	/**
	 * @param ProdPrice the product price to set
	 */
	public void setPrice(double ProdPrice){
		this.ProdPrice = ProdPrice;
	}

	/**
	 * @return the ProdName
	 */
	public String getName(){
		return ProdName;
	}

	/**
	 * @param ProdName the product name to set
	 */
	public void setName(String ProdName){
		this.ProdName = ProdName;
	}

	/**
	 * @return the ProdDescription
	 */
	public String getDescription(){
		return ProdDescription;
	}

	/**
	 * @param ProdDescription the product description to set
	 */
	public void setDescription(String ProdDescription){
		this.ProdDescription = ProdDescription;
	}

	/**
	 * @return the CategoryID
	 */
	public long getCategoryID(){
		return CategoryID;
	}

	/**
	 * @param CategoryID the CategoryID to set
	 */
	public void setCategoryID(long CategoryID){
		this.CategoryID = CategoryID;
	}

	/**
	 * This function returns product details as String Array
	 */
	public String[] toArrString(){
		try{
			String[] productarr = new String[4];
			productarr[0] =  String.valueOf(getPrice());
			productarr[1] = this.getName();
			productarr[2] = this.getDescription();
			productarr[3] = String.valueOf(getCategoryID());
			return productarr;
		}
		catch(Exception e){
			return null;
		}
	}
	
	/**
	 * This function loads a product from DB
	 * @param CategoryID the id of the product to load
	 * @return true on success, false on fail
	 * @throws ClientNotInitializesException
	 */
	public boolean loadProduct(long CategoryID) throws ClientNotInitializesException{		
		ConnectionToServer connectiontoserver = new ConnectionToServer();
		try{
			String[] productinfo = (String[]) connectiontoserver.loadProduct(CategoryID);
			if (productinfo == null)
				return false;	
			setCategoryID(Long.parseLong(productinfo[3]));
			setDescription(productinfo[2]);
			setName(productinfo[1]);
			setPrice(Double.parseDouble(productinfo[0]));
			return true;
		}
		catch(Exception e){
			return false;
		}				
	}
	
	/**
	 * This function updates a product in the DB
	 * @return String on success, null on fail
	 * @throws ClientNotInitializesException
	 */
	public String updateProduct() throws ClientNotInitializesException{		
		ConnectionToServer connectiontoserver = new ConnectionToServer();
		try{
			Object ServerMessage = connectiontoserver.updateProduct(this);
			if (ServerMessage == null)
				return null;
			return (String)ServerMessage;
		}
		catch(Exception e){
			return null;
		}				
	}
}
